package com.ysstech.etfmanage.service.impl;


import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.entity.PageBean;
import com.ysstech.etfmanage.entity.ParamVo;
import com.ysstech.etfmanage.entity.DealDetaild;
import com.ysstech.etfmanage.entity.Skep;
import com.ysstech.etfmanage.enums.EtfManageEnum;
import com.ysstech.etfmanage.mapper.DealDetaildMapper;
import com.ysstech.etfmanage.service.DealDetaildService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-02-25
 */
@Service
public class DealDetaildServiceImpl extends ServiceImpl<DealDetaildMapper, DealDetaild> implements DealDetaildService {

    /**
     * 查询ETF成交明细列表信息
     *
     * @param dealDetaild
     * @param pageBean
     * @return
     * @throws Exception
     */
    @Override
    public PageBean<Skep> queryAllEtfDealDetaild(DealDetaild dealDetaild, PageBean pageBean) throws Exception {
        QueryWrapper<DealDetaild> wrapper = this.getQueryWrapper(dealDetaild);
        IPage<DealDetaild> pageDealDetaild = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListEtfaccount(pageDealDetaild.getRecords());
        return new PageBean<>(pageDealDetaild);
    }

    /**
     * 类型等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public List<DealDetaild> switchedListEtfaccount(List<DealDetaild> list) throws Exception {
        for (DealDetaild dealDetaild : list) {
            if (null == dealDetaild) {
                continue;
            }
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param dealDetaild
     * @return
     * @throws Exception
     */
    private QueryWrapper<DealDetaild> getQueryWrapper(DealDetaild dealDetaild) throws Exception {
        QueryWrapper<DealDetaild> wrapper = new QueryWrapper<>();
        if (null == dealDetaild) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(dealDetaild.getSelfdate())) {
            wrapper.eq("FDATE", dealDetaild.getSelfdate());
        }
        return wrapper;
    }

    /**
     * 获取特定日期的 申购赎回总的替代金额和数量
     *
     * @param fdate
     * @param paramVo
     * @return
     */
    public List<DealDetaild> querySumReplaceMoneyAndAmount(String fdate, ParamVo paramVo) {
        //获取一级代码的申购和赎回的总数量
        QueryWrapper<DealDetaild> wrapperDetailedAmount = new QueryWrapper<>();
        wrapperDetailedAmount.select("SUM(DEAL_AMOUNT) as dealAmount", "BUSINESS_TYPE");
        wrapperDetailedAmount.eq("ETF_MARKET", paramVo.getEtfMarket());
        wrapperDetailedAmount.eq("ETF_MODE", paramVo.getEtfMode());
        wrapperDetailedAmount.eq("ETF_TYPE", paramVo.getEtfType());
        wrapperDetailedAmount.eq("BUSINESS_CATEGORY", "YJDM");
        wrapperDetailedAmount.eq("FDATE", fdate);
        wrapperDetailedAmount.groupBy("BUSINESS_TYPE");
        List<DealDetaild> listAmount = this.list(wrapperDetailedAmount);
        //获取资金代码的申购的赎回的总金额
        QueryWrapper<DealDetaild> wrapperDetailedMoney = new QueryWrapper<>();
        wrapperDetailedMoney.select("SUM(DEAL_MONEY) as dealMoney", "BUSINESS_TYPE");
        wrapperDetailedMoney.eq("ETF_MARKET", paramVo.getEtfMarket());
        wrapperDetailedMoney.eq("ETF_MODE", paramVo.getEtfMode());
        wrapperDetailedMoney.eq("ETF_TYPE", paramVo.getEtfType());
        wrapperDetailedMoney.eq("BUSINESS_CATEGORY", "ZJDM");
        wrapperDetailedMoney.eq("FDATE", fdate);
        wrapperDetailedMoney.groupBy("BUSINESS_TYPE");
        List<DealDetaild> listMoney = this.list(wrapperDetailedMoney);
        for (DealDetaild dealDetaild : listAmount) {
            if (null == dealDetaild) {
                continue;
            }
            for (DealDetaild dealDetaild2 : listMoney) {
                if (null == dealDetaild2 || !dealDetaild.getBusinessType().equals(dealDetaild2.getBusinessType())) {
                    continue;
                }
                if ("S".equals(dealDetaild.getBusinessType())) {
                    dealDetaild.setBusinessType(EtfManageEnum.ETF_PURCHASE.getCode());
                } else if ("B".equals(dealDetaild.getBusinessType())) {
                    dealDetaild.setBusinessType(EtfManageEnum.ETF_REDEMPTION.getCode());
                }
                dealDetaild.setDealMoney(dealDetaild2.getDealMoney());
            }
        }
        return listAmount;
    }


}
